// admin/system/service/sys_dict_data_service.go
package service

import (
	"errors"
	repo "go-admin1/admin/system/dao"
	"go-admin1/admin/system/model"
)

// SysDictDataService 字典数据服务接口
type SysDictDataService interface {
	Create(dictData *model.SysDictData) error
	GetByID(id int64) (*model.SysDictData, error)
	Update(dictData *model.SysDictData) error
	Delete(id int64) error
	List(page, pageSize int) ([]*model.SysDictData, int64, error)
	ListByDictType(dictType string) ([]*model.SysDictData, error)
	ListByDictTypeAndStatus(dictType, status string) ([]*model.SysDictData, error)
}

type sysDictDataService struct {
	dictDataDao repo.SysDictDataDao
}

func (s sysDictDataService) Create(dictData *model.SysDictData) error {
	return s.dictDataDao.Create(dictData)
}

func (s sysDictDataService) GetByID(id int64) (*model.SysDictData, error) {
	dictData, err := s.dictDataDao.GetByID(id)
	if err != nil {
		return nil, err
	}
	return dictData, nil
}

func (s sysDictDataService) Update(dictData *model.SysDictData) error {
	existingDictData, err := s.dictDataDao.GetByID(dictData.DictCode)
	if err != nil {
		return err
	}
	if existingDictData == nil {
		return errors.New("字典数据不存在")
	}

	return s.dictDataDao.Update(dictData)
}

func (s sysDictDataService) Delete(id int64) error {
	existingDictData, err := s.dictDataDao.GetByID(id)
	if err != nil {
		return err
	}
	if existingDictData == nil {
		return errors.New("字典数据不存在")
	}

	return s.dictDataDao.Delete(id)
}

func (s sysDictDataService) List(page, pageSize int) ([]*model.SysDictData, int64, error) {
	dictData, total, err := s.dictDataDao.List(page, pageSize)
	if err != nil {
		return nil, 0, err
	}
	return dictData, total, nil
}

func (s sysDictDataService) ListByDictType(dictType string) ([]*model.SysDictData, error) {
	dictData, err := s.dictDataDao.ListByDictType(dictType)
	if err != nil {
		return nil, err
	}
	return dictData, nil
}

func (s sysDictDataService) ListByDictTypeAndStatus(dictType, status string) ([]*model.SysDictData, error) {
	dictData, err := s.dictDataDao.ListByDictTypeAndStatus(dictType, status)
	if err != nil {
		return nil, err
	}
	return dictData, nil
}

// NewSysDictDataService 创建字典数据服务实例
func NewSysDictDataService() SysDictDataService {
	return &sysDictDataService{
		dictDataDao: repo.NewSysDictDataDao(),
	}
}
